import api from './auth'

// 用户管理API
export const userAPI = {
  // 获取所有用户，支持条件筛选
  getUsers: (params = {}) => {
    // params: { account, isActived }
    const query = [];
    if (params.account) query.push(`account=${encodeURIComponent(params.account)}`);
    if (params.isActived !== undefined && params.isActived !== null) query.push(`isActived=${params.isActived}`);
    const queryString = query.length ? `?${query.join('&')}` : '';
    return api.get(`/user${queryString}`);
  },

  // 根据ID获取用户
  getUserById: (id) => {
    return api.get(`/user/${id}`)
  },

  // 根据账号获取用户
  getUserByAccount: (account) => {
    return api.get(`/user/${account}`)
  },

  // 添加用户
  addUser: (data) => {
    return api.post('/user', {
      account: data.account,
      password: data.password,
      defaultRole: data.defaultRole || 'User',
      departmentId: data.departmentId,
      isDeleted: data.isDeleted,
      isActived: data.isActived,
      remarks: data.remarks,
      displayName: data.displayName,
      email: data.email,
      phone: data.phone
    })
  },

  // 更新用户
  updateUser: (id, data) => {
    return api.put(`/user/${id}`, {
      account: data.account,
      password: data.password,
      defaultRole: data.defaultRole,
      departmentId: data.departmentId,
      isActived: data.isActived,
      isDeleted: data.isDeleted,
      remarks: data.remarks
    })
  },

  // 删除用户
  deleteUser: (id) => {
    return api.delete(`/user/${id}`)
  },

  // 修改密码
  changePassword: (data) => {
    return api.post('/user/change-password', {
      currentPassword: data.currentPassword,
      newPassword: data.newPassword
    })
  },

  // 更新个人资料
  updateProfile: (data) => {
    return api.put('/user/profile', {
      displayName: data.displayName,
      email: data.email,
      phone: data.phone
    })
  },

  // 上传头像
  uploadAvatar: (formData) => {
    return api.post('/user/upload-avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  sendResetCode: (data) => {
    return api.post('/user/send-reset-code', data)
  },
  resetPassword: (data) => {
    return api.post('/user/reset-password', data)
  }
}

export default userAPI
